package cn.xiaoxiang.org.controller;

import cn.xiaoxiang.basic.annotation.PreAuthorize;
import cn.xiaoxiang.basic.util.AjaxResult;
import cn.xiaoxiang.basic.util.PageList;
import cn.xiaoxiang.org.domain.Department;
import cn.xiaoxiang.org.query.DepartmentQuery;
import cn.xiaoxiang.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
@PreAuthorize
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;
    @GetMapping()
    @PreAuthorize(name = "全部查询",sn="department:getAll")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询详情" )
    @PreAuthorize(name = "根据Id查询",sn="department:get")
    public Department findById(@ApiParam(value="部门id",required = true) @PathVariable("id") Long id) {
        return departmentService.findById(id);

    }
    //刪除
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门",sn="department:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    //批量刪除
    @PatchMapping
    @PreAuthorize(name = "批量删除部门",sn="department:patch")
    public AjaxResult batchdelete(@RequestBody List<Long> ids) {
        departmentService.batchdelete(ids);
        return AjaxResult.me().success();
    }
//添加或者修改
@PutMapping()
@PreAuthorize(name = "添加或者修改部门",sn="department:put")
public AjaxResult addOrUpdate(@RequestBody Department department) {
    if (null != department.getId()) {
        // 修改
        departmentService.update(department);
    } else {
        // 添加
        departmentService.add(department);
    }
    return AjaxResult.me().success();
}
    //分頁
    @PostMapping()
    @PreAuthorize(name = "分页查询",sn="department:post")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        return departmentService.queryByPage(query);
    }

    @GetMapping("/tree")
    public List<Department> tree() {
        return departmentService.tree();
    }

    }
